package ir.mirrajabi.searchdialog;

import ir.mirrajabi.searchdialog.adapters.SearchDialogAdapter;
import ir.mirrajabi.searchdialog.core.BaseFilter;
import ir.mirrajabi.searchdialog.core.BaseSearchDialogCompat;
import ir.mirrajabi.searchdialog.core.FilterResultListener;
import ir.mirrajabi.searchdialog.core.OnPerformFilterListener;
import ir.mirrajabi.searchdialog.core.SearchResultListener;
import ir.mirrajabi.searchdialog.core.Searchable;
import ir.mirrajabi.searchdialog.util.WindowUtil;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ListContainer;
import ohos.agp.components.ProgressBar;
import ohos.agp.components.Text;
import ohos.agp.components.TextField;
import ohos.agp.components.TextFilter;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.utils.TextTool;
import ohos.app.Context;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

import java.util.ArrayList;

/**
 * Created by MADNESS on 5/14/2017.
 * 过滤弹框面板
 */
public class SimpleSearchDialogCompat<T extends Searchable> extends BaseSearchDialogCompat<T> {
    private String mTitle;
    private String mSearchHint;
    private SearchResultListener<T> mSearchResultListener;

    private Text mTxtTitle;
    private TextField mSearchBox;
    private ListContainer listContainer;
    private ProgressBar mProgressBar;
    private boolean mCancelOnTouchOutside = true;

    // In case you are doing process in another thread
    // and wanted to update the progress in that thread
    private EventHandler mHandler;

    public SimpleSearchDialogCompat(
            Context context,
            ArrayList<T> items,
            TextFilter filter,
            BaseItemProvider adapter,
            FilterResultListener filterResultListener) {
        super(context, items, filter, adapter, filterResultListener);
    }

    public SimpleSearchDialogCompat(
            Context context,
            String title,
            String searchHint,
            TextFilter filter,
            ArrayList<T> items,
            SearchResultListener<T> searchResultListener) {
        super(context, items, filter, null, null);
        init(title, searchHint, searchResultListener);
    }

    private void init(String title, String searchHint, SearchResultListener<T> searchResultListener) {
        mTitle = title;
        mSearchHint = searchHint;
        mSearchResultListener = searchResultListener;
        setFilterResultListener(
                new FilterResultListener<T>() {
                    @Override
                    public void onFilter(ArrayList<T> items) {
                        String key = mSearchBox.getText().toString();
                        ((SearchDialogAdapter) getAdapter())
                                .setSearchTag(TextTool.isNullOrEmpty(key) ? null : key)
                                .setItems(items);
                    }
                });
        mHandler = new EventHandler(EventRunner.getMainEventRunner());
    }

    @Override
    protected void getView(Component view) {
        mTxtTitle = (Text) view.findComponentById(ResourceTable.Id_txt_title);
        mSearchBox = (TextField) view.findComponentById(getSearchBoxId());
        listContainer = (ListContainer) view.findComponentById(getRecyclerViewId());
        mProgressBar = (ProgressBar) view.findComponentById(ResourceTable.Id_rProgress);
        mTxtTitle.setText(mTitle);
        mSearchBox.setHint(mSearchHint);
        mProgressBar.setIndeterminate(true);
        mProgressBar.setVisibility(Component.HIDE);
        view.findComponentById(ResourceTable.Id_stack_root)
                .setClickedListener(
                        new Component.ClickedListener() {
                            @Override
                            public void onClick(Component component) {
                                if (mCancelOnTouchOutside) {
                                    hide();
                                    destroy();
                                }
                            }
                        });
        final SearchDialogAdapter adapter =
                new SearchDialogAdapter<>(mContext, ResourceTable.Layout_search_list_item, getItems());
        adapter.setSearchResultListener(mSearchResultListener);
        adapter.setSearchDialog(this);
        setFilterResultListener(getFilterResultListener());
        setAdapter(adapter);
        mSearchBox.requestFocus();
        ((BaseFilter<T>) getFilter())
                .setOnPerformFilterListener(
                        new OnPerformFilterListener() {
                            @Override
                            public void doBeforeFiltering() {
                                setLoading(true);
                            }

                            @Override
                            public void doAfterFiltering() {
                                setLoading(false);
                            }
                        });

        setAlignment(LayoutAlignment.TOP);
        setSize(WindowUtil.getWindowWdith(mContext), WindowUtil.getWindowHeight(mContext));
        setTransparent(true);
        setContentCustomComponent(view);
    }

    /**
     * 设置输入框hint
     * @param searchHint searchHint
     * @return SimpleSearchDialogCompat
     */
    public SimpleSearchDialogCompat setSearchHint(String searchHint) {
        mSearchHint = searchHint;
        if (mSearchBox != null) {
            mHandler.postTask(
                    new Runnable() {
                        @Override
                        public void run() {
                            mSearchBox.setHint(mSearchHint);
                        }
                    });
        }
        return this;
    }

    /**
     * 加载中
     * @param isLoading isLoading
     */
    public void setLoading(final boolean isLoading) {
        mHandler.postTask(
                new Runnable() {
                    @Override
                    public void run() {
                        if (mProgressBar != null && listContainer != null) {
                            listContainer.setVisibility(!isLoading ? Component.VISIBLE : Component.HIDE);
                        }
                        if (listContainer != null && mProgressBar != null) {
                            mProgressBar.setVisibility(isLoading ? Component.VISIBLE : Component.HIDE);
                        }
                    }
                });
    }

    /**
     * 监听结果
     * @param searchResultListener searchResultListener
     * @return SimpleSearchDialogCompat
     */
    public SimpleSearchDialogCompat setSearchResultListener(SearchResultListener<T> searchResultListener) {
        mSearchResultListener = searchResultListener;
        return this;
    }

    /**
     * 布局id
     * @return int
     */
    @Override
    public int getLayoutResId() {
        return ResourceTable.Layout_search_dialog_compat;
    }

    /**
     * 获取输入框id
     * @return int
     */
    @Override
    protected int getSearchBoxId() {
        return ResourceTable.Id_tf_input;
    }

    /**
     * getRecyclerViewId
     * @return int
     */
    @Override
    protected int getRecyclerViewId() {
        return ResourceTable.Id_search_list_container;
    }

    /**
     * getSearchBox
     * @return TextField
     */
    public TextField getSearchBox() {
        return mSearchBox;
    }

    /**
     * 标题
     * @return String
     */
    public String getTitle() {
        return mTitle;
    }

    /**
     * 设置标题
     * @param title title
     * @return SimpleSearchDialogCompat
     */
    public SimpleSearchDialogCompat setTitle(String title) {
        mTitle = title;
        if (mTxtTitle != null) {
            mHandler.postTask(
                    new Runnable() {
                        @Override
                        public void run() {
                            mTxtTitle.setText(mTitle);
                        }
                    });
        }
        return this;
    }

    /**
     * 获取列表
     * @return ListContainer
     */
    public ListContainer getListContainer() {
        return listContainer;
    }

    /**
     * getProgressBar
     * @return ProgressBar
     */
    public ProgressBar getProgressBar() {
        return mProgressBar;
    }

    /**
     * getTitleTextView
     * @return Text
     */
    public Text getTitleTextView() {
        return mTxtTitle;
    }

    /**
     * willCancelOnTouchOutside
     * @return boolean
     */
    public boolean willCancelOnTouchOutside() {
        return mCancelOnTouchOutside;
    }

    /**
     * 设置触摸关闭
     * @param cancelOnTouchOutside cancelOnTouchOutside
     */
    public void setCancelOnTouchOutside(boolean cancelOnTouchOutside) {
        mCancelOnTouchOutside = cancelOnTouchOutside;
    }
}
